package cmj.cloudmusic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import cmj.cloudmusic.R;
import cmj.cloudmusic.entity.MusicMenuType;
import cmj.cloudmusic.entity.MyMusicMenu;

/**
 * Created by 王虎 on 2016/12/13.
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    Context context;
    Map<String,List<MyMusicMenu>> map;
    List<MusicMenuType> list;

    public MyExpandableListAdapter(Context context, Map<String, List<MyMusicMenu>> map, List<MusicMenuType> list) {
        this.map = map;
        this.list = list;
        this.context=context;
    }

    @Override
    public int getGroupCount() {
        return map.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return map.get(list.get(groupPosition).getName()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return map.get(list.get(groupPosition).getName());
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return map.get(list.get(groupPosition).getName()).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.expandable_farher_item, null);
        }
        view.setTag(R.layout.expandable_farher_item, groupPosition);
        view.setTag(R.layout.expandable_child1_item, -1);
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView count = (TextView) view.findViewById(R.id.count);
        name.setText(list.get(groupPosition).getName());
        count.setText("("+list.get(groupPosition).getCount()+")");
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.expandable_child1_item, null);
        }
        view.setTag(R.layout.expandable_farher_item, groupPosition);
        view.setTag(R.layout.expandable_child1_item, childPosition);
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView count = (TextView) view.findViewById(R.id.count);
        name.setText(map.get(list.get(groupPosition).getName()).get(childPosition).getTitle());
        count.setText(map.get(list.get(groupPosition).getName()).get(childPosition).getCount()+"首");

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class PViewHolder{
        TextView title;
        TextView count;
        public PViewHolder(View v){
            title= (TextView) v.findViewById(R.id.name);
            count= (TextView) v.findViewById(R.id.count);
        }
        public void update(MusicMenuType m){
            title.setText(m.getName());
            count.setText("("+m.getCount()+")");
        }
    }
}
